const reqlib = require('app-root-path').require
const binanceProxy = reqlib('/app/common/binanceProxy')
const MarketMachine = require('./marketMachine')
const positionCalculate = require('./utils/positionCalculate')

function AccountMachine (config) {
  this.btbCount = config.btbCount
  this.positionConfig = config.positionConfig || 1
  this.dictionaryPosition = config.dictionaryPosition || ''
  this.marketInfo = false
  this.marketFlagInfo = {}
  this.marketMachine = {}
  this.marketInfoList = config.marketInfoList || []
  if (config.marketInfo && Object.keys(config.marketInfo).length > 0) {
    this.marketInfo = config.marketInfo
    this.marketMachine = new MarketMachine({
      marketInfo: this.marketInfo,
      marketInfoList: this.marketInfoList
    })
    this.marketFlagInfo = this.marketMachine.getAllFlag()
  }
  // 做保守一点，其实除了reason，其他的交易基本都是亏钱的
  // 钱基本是reason挣得，其他时候就是低仓混着，做积累的
  // reason太争气了，胜率高，盈亏比也好
  this.countPositionResultConservative()
  this.info = {
    btbCount: this.btbCount
  }
  this.setPosition()
}

AccountMachine.prototype = {
  ...positionCalculate,
  setPosition () {
    this.info.positionConfig = this.positionConfig
    this.info.dictionaryPosition = this.dictionaryPosition
    this.info.btbCount = this.btbCount
  },
  async refresh () {
    this.info = await binanceProxy.getFuturesAccount()
    this.info.totalWalletBalance = this.info.totalWalletBalance * this.positionConfig
    // 标准保证金，包含仓位配置的
    this.info.standardInitialMargin = this.info.totalWalletBalance / this.btbCount
    // 没有计算仓位配置过的
    this.info.rawStandardInitialMargin = this.info.rawTotalWalletBalance / this.btbCount
    // 最大保证金
    this.info.maxStandardInitialMargin = this.info.rawTotalWalletBalance / 10
    // 币的数量
    this.info.btbCount = this.btbCount
    this.setPosition()
  },
  getFuturesAccount () {
    this.setPosition()
    return this.info
  },
  getHasPositionSymbolList () {
    return this.info.hasPositionSymbolList || []
  },
  getBtbItemAccount (key) {
    const positionMap = this.info.positionMap || {}
    const info = positionMap[key] || {}
    info.positionAmt = info.positionAmt || 0
    info.notional = info.notional || 0
    info.unrealizedProfit = info.unrealizedProfit || 0
    info.initialMargin = info.initialMargin || 0
    info.incomeRate = info.incomeRate || 0
    info.incomeMarginRate = info.incomeMarginRate || 0
    info.hasPosition = Math.abs(info.notional) > 2
    info.hasDuoPosition = info.hasPosition && info.notional > 0
    info.hasKongPosition = info.hasPosition && info.notional < 0
    // 标准的带仓位
    info.standardInitialMargin = this.info.standardInitialMargin
    // 原始的不带仓位
    info.rawStandardInitialMargin = this.info.rawStandardInitialMargin
    // 最大的
    info.maxStandardInitialMargin = this.info.maxStandardInitialMargin
    // 仓位
    // 到了大后期，仓位分配应该根据成交量分配
    info.position = info.initialMargin / info.standardInitialMargin
    info.rawPosition = info.initialMargin / info.rawStandardInitialMargin
    info.maxStandardPosition = info.initialMargin / info.maxStandardInitialMargin
    return info
  }
}

module.exports = AccountMachine
